package jmine.tec.scheduling.services.alterar;

import jmine.tec.di.annotation.Injected;
import jmine.tec.scheduling.domain.ScheduledTask;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.RawInput;

/**
 * Service for scheduled task update
 * 
 * @author ricardo.abe
 */
@ServiceImplementor(action = ActionsEnum.ALTERAR)
public class ScheduledTaskService {
    public static final String TASK = "Task";

    public static final String DESCRIPTION = "Descrição";

    public static final String ACTIVE = "Ativo";

    public static final String CRON = "Cron expression";

    public static final String CONDITION = "Condição";

    public static final String ACTION = "Ação";

    private ScheduledTask task;

    private String description;

    private Boolean active;

    private String cron;

    private String condition;

    private String action;

    private RawInput rawInput;

    /**
     * Execute do Servico.
     */
    @Execution
    public void execute() {
        if (this.description != null) {
            this.task.setDescription(this.description);
        }
        if (this.active != null) {
            this.task.setActive(this.active);
        }
        if (this.cron != null) {
            this.task.setCron(this.cron);
        }
        this.condition = this.rawInput.getRaw(CONDITION);
        if (this.condition != null) {
            this.task.setCondition(this.condition);
        }
        this.action = this.rawInput.getRaw(ACTION);
        if (this.action != null) {
            this.task.setAction(this.action);
        }
        this.task.getPersister().save();
    }

    /**
     * @param task the task to set
     */
    @Input(fieldName = TASK)
    public void setTask(ScheduledTask task) {
        this.task = task;
    }

    /**
     * @param description the description to set
     */
    @Input(fieldName = DESCRIPTION, required = false)
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @param active the active to set
     */
    @Input(fieldName = ACTIVE, required = false)
    public void setActive(Boolean active) {
        this.active = active;
    }

    /**
     * @param cron the cron to set
     */
    @Input(fieldName = CRON, required = false)
    public void setCron(String cron) {
        this.cron = cron;
    }

    /**
     * @param rawInput raw input.
     */
    @Injected
    public void setRawInput(RawInput rawInput) {
        this.rawInput = rawInput;
    }
}
